package com.tiancheng.trade.merchant.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.merchant.exception.BusinessException;
import com.tiancheng.trade.merchant.model.Organization;
import com.tiancheng.trade.merchant.service.IOrganizationService;
import com.tiancheng.trade.merchant.util.ResultJudgeUtil;
import com.tiancheng.trade.merchant.vo.merchant.QueryOrgPageResVO;
import com.tiancheng.trade.merchant.vo.organization.CreateOrganizationVO;
import com.tiancheng.trade.merchant.vo.organization.UpdateOrganizationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/admin/organization")
public class OrganizationAdminController {
    @Autowired
    private IOrganizationService organizationService;

    /**
     * 分页查询组织机构信息
     *
     * @param organizationVO {@link Organization}
     * @return {@link Result}<{@link PageInfo}<{@link Organization}>>
     */
    @GetMapping("/findPageList")
    public Result<PageInfo<QueryOrgPageResVO>> findPageList(Organization organizationVO) throws BusinessException {
        return Result.success(organizationService.findPageList(organizationVO));
    }

    /**
     * 查询全部组织机构信息
     *
     * @return {@link Result}<{@link List}<{@link Organization}>>
     */
    @GetMapping("/all")
    public Result<List<Organization>> all() throws BusinessException {
        return Result.success(organizationService.findAll());
    }

    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable("id") Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return ResultJudgeUtil.judge(organizationService.remove(id), "删除");
    }

    @GetMapping("/{id}")
    public Result<Organization> findById(@PathVariable("id") Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return Result.success(organizationService.findDetail(id));
    }

    /**
     * 修改
     *
     * @param organizationVO {@link UpdateOrganizationVO}
     */
    @PutMapping("/")
    public Result<?> update(@RequestBody UpdateOrganizationVO organizationVO) throws BusinessException {
        Assert.isTrue(organizationVO.getId() != null, "参数错误~");
        Organization org = new Organization();
        BeanUtils.copyProperties(organizationVO, org);
        return ResultJudgeUtil.judge(organizationService.update(org), "修改");
    }

    /**
     * 新增
     *
     * @param organizationVO {@link CreateOrganizationVO}
     */
    @PostMapping("/")
    public Result<?> create(@RequestBody CreateOrganizationVO organizationVO) throws BusinessException {
        Organization org = new Organization();
        BeanUtils.copyProperties(organizationVO, org);
        return ResultJudgeUtil.judge(organizationService.create(org), "新增");
    }

    /**
     * 查询组织列表信息
     *
     * @param organization {@link Organization}
     */
    @PostMapping("/export")
    public void merchantExport(@RequestBody Organization organization, HttpServletRequest request,
                               HttpServletResponse response) throws IOException {

        organizationService.export(organization, request, response);
    }

}
